﻿namespace EasyXaf.EntityDesigner.Commands;

public class SelectEntityCommand : ICommand
{
    public bool CanExecute(string command)
    {
        if (!string.IsNullOrWhiteSpace(command))
        {
            return command.StartsWith('>');
        }
        return false;
    }

    public void Execute(string command, CommandContext context)
    {
        var entityName = command[1..].Trim().Split(' ')[0];
        if (!string.IsNullOrWhiteSpace(entityName))
        {
            var entity = context.DomainModel.VisualEntities.FirstOrDefault(e => e.Name.Equals(entityName, StringComparison.OrdinalIgnoreCase));
            if (entity != null)
            {
                context.CurrentEntity = entity;
            }
            else
            {
                throw new CommandException(command, $"没有找到实体 '{entityName}'");
            }
        }
    }
}
